import { Component, OnInit  } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'
import axios from 'axios';

@Component({
  selector: 'app-subroom',
  templateUrl: './subroom.component.html',
  styleUrls: ['./subroom.component.css']
})
export class SubroomComponent implements OnInit {
  isDataLoaded = false
  roomId 
  room 
  private timer
  user
  constructor(public route:ActivatedRoute, public router:Router) { 
    console.log("constructor")
    this.route.params.subscribe((params)=>{
      //console.log(params);
      this.roomId = params.id;
    })

    //this.getRoomData();
    this.getCurrentUser();

    this.timer = setInterval(() => {
      this.getRoomData();
    }, 1000);

  }

  public async getRoomData(){
    if (!this.user) return 

    let baseUrl = "http://localhost:8999";


    await  axios.get(baseUrl+'/room/getRoom?roomId='+this.roomId+"&userId="+this.user.userId, ).then(res=>{
      //this.room = res.data;
      this.sortQuestion(res.data)
      
      this.isDataLoaded =true

      }).catch((err)=>{
        console.log(err);
     }).finally(()=>{
        console.log('执行完了');
     });

  }

  private sortQuestion(room){
     let questions = room.questions

     questions.sort(function (a, b) {
      return a.questionId - b.questionId;
    });
    room.questions = questions
    this.room = room;
    //console.log(room)

  }

  public async addLike(questionId, event){
    event.target.style.disabled='true'
    console.log("questionId" +questionId);

    var params = new URLSearchParams()
    params.append('questionId', questionId)
    params.append('userId', this.user.userId)

    let baseUrl = "http://localhost:8999";

    axios.post(baseUrl+'/question/like', params)
    .then(function (response) {
      //console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });

  }

  public createQuestion(obj){
    //console.log(obj.value.question)

    var params = new URLSearchParams()
    params.append('roomId', this.roomId)
    params.append('question', obj.value.question)
    params.append('userId', this.user.userId)
    this.getData(params, "/question/createQuestion" );
  
  }

  ngOnInit(): void {
    console.log("ngOnInit")

  }

  public getCurrentUser(){
    let baseUrl = "http://localhost:8999";

     axios.get(baseUrl+'/user/getCurrentUser', {withCredentials: true}).then(res=>{
      this.user = res.data;
      console.log(res.data)
      this.getRoomData()

      //this.isDataLoaded =true
      }).catch((err)=>{
        console.log(err);
     }).finally(()=>{
        console.log('执行完了');
     });
  }

  public async getData(params:URLSearchParams, url:String){
      let baseUrl = "http://localhost:8999";
      await  axios.post(baseUrl+url, params)
      .then(function (response) {
        console.log(response);
        return response;
      })
      .catch(function (error) {
        console.log(error);
      });
  }


  //销毁组件时清除定时器
  ngOnDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }




}
